package com.sdvdxl.beans;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.sdvdxl.exceptios.NetworkException;
import com.sdvdxl.exceptios.UnGrantException;
import com.sdvdxl.oauth.GrantManager;

/**
 * 用户关系类<br>
 * 本类可以查看好友信息，用户与某用户(uid)的好友关系<br>
 * 详细信息请查阅本类具体方法说明
 * @author sdvdxl (sdvdxl@163.com)
 *
 */
public class Relationships {
	private List<User> friendsList = new ArrayList<User>();
	
	/**
	 * 根据获取好友列表
	 * @param user
	 * @return
	 * @throws UnGrantException 
	 * @throws NetworkException 
	 */
	public List<User> getFriendsList() throws UnGrantException, NetworkException {
		if (GrantManager.ACCESS_TOKEN==null) {
			throw new UnGrantException("没有权限，未取得access_token");
		}
		getFriends();
		return friendsList;
	}
	
	/**
	 * 查看任意两人是否是好友关系<br>
	 * 若follower是host的好友，返回true<br>
	 * 若follower不是host的好友，返回false
	 * 
	 * @param hostUid
	 * @param followerUid
	 * @return
	 * @throws NetworkException 
	 */
	public boolean areFriends(String hostUid, String followerUid) throws NetworkException {
		String xmlURL = "https://openapi.baidu.com/rest/2.0/friends/areFriends?" +
				"access_token=" + GrantManager.ACCESS_TOKEN
				+ "&format=xml"
				+ "&uids1=" + hostUid 
				+ "&uids2=" + followerUid;
		
		Document doc = null;
		String basic = "//friends_areFriends_response/";
		
		try {
			URL url = new URL(xmlURL);
			URLConnection uc = url.openConnection();
			doc = new SAXReader().read(uc.getInputStream());
		} catch (DocumentException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			throw new NetworkException("网络异常");
		}
		
		String inf = doc.selectSingleNode(basic + "friend_info/are_friends").getText().trim();
		if ("1".equals(inf)) {
			return true;
		}
		return false;
	}
	
	
	/**
	 * 查看任意两人是否是好友关系<br>
	 * 若follower是host的好友，返回true<br>
	 * 若follower不是host的好友，返回false
	 * 
	 * @see areFriends(String hostUid, String followerUid)
	 * @param host
	 * @param follower
	 * @return follower和host的好友关系
	 * @throws NetworkException 
	 */
	public boolean areFriends(User host, User follower) throws NetworkException {
		return areFriends(host.getUid(), follower.getUid());
	}
	
	/**
	 * 取得用户的所有好友
	 * @throws UnGrantException 
	 * @throws NetworkException 
	 */
	private void getFriends() throws UnGrantException, NetworkException {
		final String  xmlURL = "https://openapi.baidu.com/rest/2.0/friends/getFriends?" 
				+ "access_token=" + GrantManager.ACCESS_TOKEN + "&format=xml"
				+ "&page_size=1000";
		Document doc = null;
		String basic = "//friends_getFriends_response/";
		
		try {
			URL url = new URL(xmlURL);
			URLConnection uc = url.openConnection();
			doc = new SAXReader().read(uc.getInputStream());
		} catch (DocumentException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			throw new NetworkException("获取网络信息异常");
		}
		
		List<? extends Node> list = doc.selectNodes(basic + "friend");
		if (list==null) {
			throw new UnGrantException("未授权");
		}
		
		Iterator<? extends Node> it = list.iterator();
		while (it.hasNext()) {
			User user = new User();
			
			Node friend = it.next();
			String uid = friend.selectSingleNode(basic + "friend/uid").getText().trim();
			String uname = friend.selectSingleNode(basic + "friend/uname").getText().trim();
			String portrait = friend.selectSingleNode(basic + "friend/portrait").getText().trim();
			user.setUid(uid);
			user.setUname(uname);
			user.setPortrait("http://himg.bdimg.com/sys/portraitn/item/" + portrait + ".jpg");
			
			friendsList.add(user);
		}
		
	}
}
